/**
 * 
 */
package com.base.myproject.client.busi.b2c.shopping;


import com.base.myproject.client.busi.b2c.B2cLogin;
import com.base.myproject.client.busi.b2c.MainPage;
import com.base.myproject.client.busi.b2c.ProdectDesc;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.system.SystemMessage;
import com.base.myproject.client.tools.SendServlet;
import com.base.myproject.client.tools.XML;
import com.extjs.gxt.ui.client.fx.FxConfig;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;





/**
 * @author zgw
 *2009-7-26 下午08:30:41
 * javazgw@gmail.com
 */
public class ShoppingCar {
	
	java.util.Vector<ProdectDesc> prodectdescV=new java.util.Vector<ProdectDesc>();
	public ShoppingCar()
	{
		
	}
	/**
	 * 根据商品的id号将商品加入购物车，如果未登录这要提示先登录
	 * 
	 */
	public void add2Car(final String id)
	{
		
		if(Env.getUid()==null)
		{
			B2cLogin b2clogin = new B2cLogin(){
				protected	boolean login()
				{
					if(super.login())
					{
						ShoppingCar shopingcar = new ShoppingCar();
						shopingcar._addcar(id);
						
					return true;
					}
					else
						return false;
				}
			};
			
			Dialog dialog = b2clogin.CreateLoginDialog();
			
			dialog.show();
		}
		else
		{
			ShoppingCar shopingcar = new ShoppingCar();
			shopingcar._addcar(id);
			
		}
	}
	private void _addcar(String id)
	{
		SendServlet ss = new SendServlet();
		try {
			ss.sendServlet("BaseServlet", "id="+id+"&Action="+SystemMessage.getMessage(2, 2), new RequestCallback()
			{

				@Override
				public void onError(Request request, Throwable exception) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onResponseReceived(Request request,
						Response response) {
					
					System.out.println(response.getText());
					Document doc = XMLParser.parse((String) response.getText());
					  NodeList list = doc.getElementsByTagName("root");
					  Node node = list.item(0);
					  Element elem = (Element) node;
				
					if( elem.getAttribute("status").equals(SystemMessage.getMessage(1, 0)))
					{
						
						 Info.display("", SystemMessage.getMessage(4, 7));  
					}
					else
					{
						Info.display("", SystemMessage.getMessage(4, 8));  
					}
				}
				
			});
		} catch (RequestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 
	 * @param prodectdesc
	 * @param add2db 为true 则添加到数据库 
	 */
	public void add2Car(ProdectDesc prodectdesc,boolean add2db)
	{
		if(add2db)
		{
			String b2cgcode = prodectdesc.getId();
			/*String b2cgname = prodectdesc.getName();
			String gcode = prodectdesc.getGcode();
			String gname = prodectdesc.getGname();
			String pric = prodectdesc.getPrice();
			String discountpric = prodectdesc.getDiscountprice();
			
			String mainimagpath = prodectdesc.getMainimagepath();
			String ccode = Env.getCcode();
		
			//System.out.println("b2cgcode "+b2cgcode+" b2cgname="+b2cgname+" gcode="+gcode+" gname="+gname+" pric "+pric+" discountpric "+discountpric+" bcode "+bcode+" name="+name);
			
			String action = "Action=addshopcaritem&b2cgcode="+b2cgcode+"&b2cgname="+b2cgname+"&gcode="+gcode+"&gname="+gname+"&pric="+pric+"&discountpric="+discountpric+"&ccode"+ccode+"&mainimagepath="+mainimagpath;
			SendServlet ss = new SendServlet();
			
			ss.getStringx("/gwtbase/BaseServlet",action);*/
			_addcar(b2cgcode);
			prodectdescV.add(prodectdesc);
//			Info.display("成功", "成功将商品加入购物车。现在你可以在购物车里查看购买\n的商品和设置购买的数量了。");
			
		}
		else
		{
			prodectdescV.add(prodectdesc);
		}
		MainPage.getShoppingcar().el().blink(FxConfig.NONE);
		
	}
	public void add2Car(ProdectDesc prodectdesc)
	{
		add2Car(prodectdesc,true);
	}
	public void remove(ProdectDesc prodectdesc)
	{
		prodectdescV.remove(prodectdesc);
	}
	
	public int getcount()
	{
		return prodectdescV.size();
	}
	
	public void readcarInStore(String bname)
	{
	String sql  = "select * from b2cshopcar where bname='"+bname+"'";
		
		SendServlet ss = new SendServlet();
		String datawinstr = ss.getStringx("/"+GWT.getModuleName()+"/BaseServlet",
				"Action=getdatasql&sql=" + sql + "");
		Document doc = XMLParser.parse((String) datawinstr);

		NodeList list = doc.getElementsByTagName("record");

		for (int i = 0; i < list.getLength(); i++) {
			NodeList recordlist = list.item(i).getChildNodes();

			Node node = list.item(i);
			Element elem = (Element) node;
			
			String b2cgcode = XML.getValue(elem, "b2cgcode");
			String b2cgname = XML.getValue(elem, "b2cgname");
			String gcode = XML.getValue(elem, "gcode");
			String gname = XML.getValue(elem, "gname");
			String shopdate = XML.getValue(elem, "shopdate");
			String qty = XML.getValue(elem, "qty");
			String price = XML.getValue(elem, "price");
			String totle = XML.getValue(elem, "totle");
			
			String discountprice = XML.getValue(elem, "discountprice");
			
			String bcode = XML.getValue(elem, "bcode");
		
			ProdectDesc pd = new ProdectDesc(b2cgcode);
			pd.setBname(b2cgname);
			pd.setGcode(gcode);
			pd.setGname(gname);
			pd.setShopdate(shopdate);
			pd.setQty(qty);
			pd.setPrice(price);
			pd.setTotle(totle);
			pd.setDiscountprice(discountprice);
			add2Car(pd,false);
	
		}
	}
	
	
}
